<template>
  <div class="container">
    <el-row class="topTitleBox">
      <el-col class="topTitle">
        <i class="icon iconfont iconfanhui1" @click="goBack"></i>
        <span class="el-dropdown-link titleStyle">{{ projectName }}</span>
        <span class="status">已开工</span>
      </el-col>
    </el-row>
    <div class="mainCon">
      <el-form
        :model="formData"
        :rules="formRules"
        ref="formData"
        label-width="120px"
        class="demo-formData"
      >
        <el-row>
          <el-form-item label="受理机构：" class="row-padding-bottom">{{
            formData.bureau
          }}</el-form-item>
        </el-row>

        <el-row>
          <el-form-item
            prop="insNo"
            label="单位编号："
            class="row-padding-bottom"
          >
            <el-input
              class="sWidth"
              v-model="formData.insNo"
              placeholder="请输入单位编号"
            />
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item
            prop="insDate"
            label="缴费日期："
            class="row-padding-bottom"
          >
            <el-date-picker
              class="sWidth"
              v-model="formData.insDate"
              type="date"
              placeholder="选择日期"
              clearable
              :style="{ width: '100%' }"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
          </el-form-item>
        </el-row>

        <el-row>
          <el-form-item
            prop="companyIndex"
            label="参保单位："
            class="row-padding-bottom"
          >
            <el-select
              v-model="companyIndex"
              class="mWidth"
              filterable
              placeholder="请选择"
              clearable
            >
              <el-option
                v-for="(item, index) in companyList"
                :key="item.id"
                :label="item.companyName"
                :value="index"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item
              prop="upLoadFile"
              label="社会保险缴费征集通知单："
              class="upLoadDomContainer"
              v-model="formData.fileList"
            >
              <uploadAnnex
                @getFileList="getFileList"
                :limitationFactor="limitationFactor"
                @getFile="getFile"
                :upLoadFile="upLoadFile"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24" v-model="formData.fileList">
            <!-- <elementImage :upLoadFile="upLoadFile"   :srcList="srcList" :oldSrcList="oldSrcList" @changeSrcList="changeSrcList"></elementImage> -->
            <uploadResult
              style="margin-left: 120px !important"
              :resultList="upLoadFile"
              @callBackResult="getFile"
            />
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item
              prop="upLoadFileInsurance"
              label="税收完税证明："
              v-model="formData.fileListInsurance"
            >
              <uploadAnnex
                @getFileList="getFileListInsurance"
                :limitationFactor="limitationFactorInsurance"
                @getFile="getFileInsurance"
                :upLoadFile="upLoadFileInsurance"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24" v-model="formData.fileListInsurance">
            <uploadResult
              style="margin-left: 120px !important"
              :resultList="upLoadFileInsurance"
              @callBackResult="getFileInsurance"
            />
          </el-col>
        </el-row>
        <el-form-item class="submitBox">
          <el-button
            type="primary"
            class="commonBut saveBtn"
            :loading="isSaveLoading"
            @click="saveForm"
            v-sdc
            >保存</el-button
          >
          <el-button class="commonBut" v-sdc @click="goBack">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import common from '../../kits/common'

export default {
  name: 'EsAddInsurance',
  // components: {
  //   emptyTable,
  //   Pagination,
  // },
  data() {
    var checkCompanyIndex = (rule, value, callback) => {
      var that = this
      if (that.companyIndex.length == 0) {
        return callback(new Error('参保单位不能为空'))
      } else {
        callback()
      }
    }

    var checkUpLoadFile = (rule, value, callback) => {
      var that = this
      if (that.upLoadFile.length == 0) {
        return callback(new Error('社会保险缴费征集通知单不能为空'))
      } else {
        callback()
      }
    }
    var checkUpLoadFile1 = (rule, value, callback) => {
      var that = this
      if (that.upLoadFileInsurance.length == 0) {
        return callback(new Error('税收完税证明不能为空'))
      } else {
        callback()
      }
    }
    return {
      isHover: false,
      filterMethod(query, item) {
        return item.label.indexOf(query) > -1
      },
      projectId: '',
      projectName: '',
      companyList: [],
      company: {},
      // addDialogFormVisible: false,

      upLoadFile: [],
      oldSrcList: [],
      dialogImageUrl: '',
      srcList: [],
      upLoadFileInsurance: [],
      oldSrcListInsurance: [],
      srcListInsurance: [],
      companyIndex: '',
      isSaveLoading: false, // 保存loading
      formData: {
        insNo: '', //保险单号  -----必传
        insDate: '', //参保日期  -----必传
        projectId: '', //项目id  -----必传
        companyName: '', //参保企业名（不包含括号内） -----必传
        companyType: '', //参保企业类型 01 05 10  -----必传
        uscc: '', //参保企业uscc  -----必传
        bureau: '', //受理机构  -----必传
        batchNo: '1', //批次号  -----必传
        fileList: [], //社会保险缴费征集通知单
        fileListInsurance: [], //保单回执
      },

      // filterField: {
      //   searchText: "",
      //   insId: ""
      // },
      formRules: {
        insNo: [
          {
            required: true,
            pattern: /^[0-9]*$/,
            message: '请输入正确的单位编号',
            trigger: 'blur',
          },
        ],
        insDate: [
          {
            required: true,
            message: '缴费不能为空',
            trigger: 'blur',
          },
        ],
        companyIndex: [
          { required: true, validator: checkCompanyIndex, trigger: 'change' },
        ],

        upLoadFile: [
          {
            required: true,
            validator: checkUpLoadFile,
            trigger: 'blur',
          },
        ],
        upLoadFileInsurance: [
          {
            required: true,
            validator: checkUpLoadFile1,
            trigger: 'blur',
          },
        ],
      },
      addInsuredPersonnel: '', //添加参保人员点击确定保存数据
      // 社会保险缴费征集通知单
      limitationFactor: {
        uploadAnnexUrl: 'api-f/files/upload', //自定义上传文件接口地址
        upLoadFileLength: 10, //自定义上传附件的个数设置
        fileSize: 5, //自定义上传文件最大设置
        formatType: 'typeFour', //自定义上传格式校验类型
        fileId: 'salaryExcel', //自定义id解决一个页面包含多个上传附件
        uploadFormId: 'salaryExcelForm', //自定义id解决一个页面包含多个上传附件
        messagetip:
          '要求格式为png，jpg，jpeg，doc，docx，xls，xlsx，pdf格式，附件大小不超过5M', //自定义报错提示语
        methodTip:
          '请上传社会保险缴费征集通知单，附件格式支持png，jpg，jpeg，doc，docx，xls，xlsx，pdf，最多上传10个附件', //自定义上传文件提示
        uploadBtnType: 'typeOne', //自定义上传按钮类型
      },
      // 保单
      limitationFactorInsurance: {
        uploadAnnexUrl: 'api-f/files/upload', //自定义上传文件接口地址
        upLoadFileLength: 10, //自定义上传附件的个数设置
        fileSize: 5, //自定义上传文件最大设置
        formatType: 'typeFour', //自定义上传格式校验类型
        fileId: 'salaryExcelInsurance', //自定义id解决一个页面包含多个上传附件
        uploadFormId: 'salaryExcelInsuranceForm', //自定义id解决一个页面包含多个上传附件
        messagetip:
          '要求格式为png，jpg，jpeg，doc，docx，xls，xlsx，pdf格式，附件大小不超过5M', //自定义报错提示语
        methodTip:
          '请上传保单回执，附件格式支持png，jpg，jpeg，doc，docx，xls，xlsx，pdf，最多上传10个附件', //自定义上传文件提示
        uploadBtnType: 'typeOne', //自定义上传按钮类型
      },
    }
  },
  activated: function () {},
  created() {
    this.projectId = this.$route.query.projectId
    this.projectName = this.$route.query.projectName
    this.getCompanyListData()
    this.formData = {}
  },
  methods: {
    mouseroverInput() {
      this.isHover = true
    },
    mouserleaveInput() {
      this.isHover = false
    },
    /**
     * @name: 获取公司信息
     * @param {type}
     * @Author: luoyong
     */
    getCompanyListData() {
      this.axios({
        method: 'post',
        url: 'api-e/cs/insdetail/expand/projectInfo',
        data: { projectId: this.projectId },
      })
        .then((resp) => {
          if (!resp.data.hasError) {
            this.companyList = resp.data.result.companyList
            this.formData.bureau = resp.data.result.bureau //参保企业uscc  -----必传
          } else {
            this.$message({
              showClose: true,
              duration: 3000,
              message: resp.data.errorMsg,
              type: 'error',
            })
          }
        })
        .catch((resp) => {})
    },
    /**
     * @name: 校验参数
     * @param {type}
     * @Author: luoyong
     */
    saveForm() {
      this.$refs.formData.validate((valid) => {
        this.formData.fileList = this.upLoadFile //this.upLoadFile;

        this.formData.fileListInsurance = this.upLoadFileInsurance //this.upLoadFile;
        this.formData.projectId = this.projectId //项目id  -----必传
        if (this.companyList[this.companyIndex]) {
          this.formData.companyName =
            this.companyList[this.companyIndex].companyName //参保企业名（不包含括号内） -----必传
          this.formData.companyType = this.companyList[this.companyIndex].type //参保企业类型 01 05 10  -----必传
          this.formData.uscc = this.companyList[this.companyIndex].uscc //参保企业uscc  -----必传
        }
        if (valid) {
          this.isSaveLoading = true
          this.saveFormInfo()
        } else {
          return false
        }
      })
    },
    /**
     * @name: 保存新增
     * @param {type}
     * @Author: luoyong
     */
    saveFormInfo() {
      const url = 'api-e/cs/ins/expand/addinsforproject'
      const {
        bureau,
        companyType,
        companyName,
        uscc,
        projectId,
        insNo,
        insDate,
        fileList,
        fileListInsurance,
      } = this.formData
      fileList.forEach((item) => {
        item.contentType = 'INS_DECLARE'
      })
      fileListInsurance.forEach((item) => {
        item.contentType = 'INS_PAY'
      })
      const files = JSON.stringify([...fileList, ...fileListInsurance])
      const params = {
        projectId,
        uscc,
        companyName,
        companyType,
        insDate,
        bureau,
        insNo,
        // declareFileList: fileList,
        // payFileList: fileListInsurance
        fileList: files,
      }
      this.axios({
        method: 'post',
        url: url,
        data: params,
      })
        .then((response) => {
          this.isSaveLoading = false
          if (!response.data.hasError) {
            this.$message({
              type: 'success',
              message: '保存成功',
            })
            this.formVisible = false
            this.goBack()
          } else {
            this.$message({
              type: 'error',
              message: response.data.errorMsg,
            })
          }
        })
        .catch((error) => {
          this.isSaveLoading = false
        })
    },
    /**
     * @name: 返回上一页
     * @param {type}
     * @Author: luoyong
     */
    goBack() {
      this.$refs['formData'].resetFields()
      this.$router.push({
        // path: "/home/EsInsurance/EsInsurance",
        name: 'EsInsurance',
        query: {
          projectId: this.projectId,
          projectName: this.projectName,
        },
      })
      this.$store.commit('CHANGE_SLIDER_NAME', {
        oldUrl: '/home/EsInsurance/EsInsurance',
        newUrl: this.$router.currentRoute.fullPath,
      })
    },
    getFileList(val) {
      this.oldSrcList.push(val)
      this.srcList.push(val)
    },
    getFile(val) {
      this.upLoadFile = val
    },
    getFileListInsurance(val) {
      this.oldSrcListInsurance.push(val)
      this.srcListInsurance.push(val)
    },
    getFileInsurance(val) {
      this.upLoadFileInsurance = val
    },
  },
}
</script>
<style scoped lang="less">
.topTitleBox {
  background-color: #fff;
}
.topTitle {
  height: 50px;
  border-bottom: 1px solid #e3e7e9;
  font: bold 18px/50px Microsoft YaHei;
  padding: 0 24px;
  .titleStyle {
    color: #333;
    font-size: 18px;
    .el-icon-caret-bottom {
      color: #71818e;
    }
  }
  .status {
    display: inline-block;
    // width: 60px;
    padding: 0 8px;
    height: 20px;
    line-height: 20px;
    margin-bottom: 10px;
    background: #009588;
    color: #fff;
    text-align: center;
    font-size: 12px;
    // border-radius: 4px;
  }
  .saveBtn {
    margin-right: 24px;
    margin-top: 15.5px;
  }
}
.activedBtn {
  background-color: #f4f4f4;
  cursor: pointer;
}
.container {
  .mainCon {
    top: 198px;
    padding: 40px 60px;
    overflow-y: auto;
    // .labelStyMin {
    //   padding-left: 0px !important;
    //   min-width: 130px;
    // }
    // .labelStyMax {
    //   padding-left: 0px !important;
    //   margin-left: 5px;
    //   min-width: 155px;
    // }
    .submitBox {
      text-align: left;
      margin-top: 18px;
    }
    .picStyle {
      width: 116px;
      margin-right: 60px;
      img {
        width: 100%;
      }
    }
    .uploadBox {
      position: relative;
      .uploadImg {
        width: 96px;
        height: 36px;
        color: #009588;
        border: 1px solid #009588;
        line-height: 10px;
      }
      .uploadImgState {
        font-size: 14px;
        color: #999999;
        margin-left: 12px;
      }
      .upLoadDom {
        position: absolute;
        top: 0;
        left: 0;
        opacity: 0;
        border-radius: 18px;
        input {
          width: 96px;
          height: 36px;
          border-radius: 18px;
          vertical-align: middle;
        }
        &:hover {
          .uploadImg {
            background-color: #0ff;
          }
        }
      }
    }
    .imgStyle {
      padding-left: 130px;
      li {
        position: relative;
        margin-right: 20px;
        margin-top: 10px;
        i {
          position: absolute;
          right: -6px;
          top: -10px;
          font-size: 20px;
        }
      }
    }
  }
}
/deep/ .el-dialog__footer {
  // background: #fff;
  // padding-bottom: 70px;
}
form.el-form.demo-formData {
  /*width: 450px;*/
  display: inline-block;
  text-align: left;
  // .el-input,
  // .el-select {
  //   width: 320px !important;
  // }
  .selectWidth {
    width: 100%;
  }
}
.el-main .el-form-item {
  margin-bottom: 20px;
}
.saveBtn {
  background-color: #009588;
  border-color: #009588;
  color: #fff;
}

.upLoadDomContainer {
  /deep/label {
    line-height: 20px;
  }
}
</style>
